package com.zrrd.learn.chapter09.collectlianxi;

import java.util.*;

/**
 * List与Set练习
 *
 * @author wrn
 * @version 1.0
 * @date 2023/8/16 10:32
 */

public class ListAndSet {

    /**
     * 计算List中的元素和
     */
    public static void question01(){

        List<Integer> list = new ArrayList<>(Arrays.asList(4, 1, 6, 2, 3, 3, 4, 4));
        int sum = 0;
        for (int i = 0; i < list.size(); i++) {
            sum+= list.get(i);
        }
        System.out.println("元素和为"+sum);
    }

    /**
     * 查找List中的最大值和最小值
     */
    public static void question02(){

        List<Integer> list = new ArrayList<>(Arrays.asList(4, 1, 6, 2, 3, 3, 4, 4));
        Collections.sort(list);
        System.out.println("最小值"+list.get(0)+"最大值"+list.get(list.size()-1));

    }

    /**
     * 移除List中特定的元素
     */
    public static void question03(){

        List<Integer> list = new ArrayList<>(Arrays.asList(4, 1, 6, 2, 3, 3, 4, 4));

        System.out.println("原list集合"+list);

        System.out.println("请输入要移除的元素");
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();

        list.remove(list.indexOf(i));

        System.out.println("移除后list集合"+list);

    }

    /**
     * 将List中的字符串按长度排序
     */
    public static void question04(){

        List<String> list = new ArrayList<>(Arrays.asList("12","123","1234","1"));
        System.out.println("排序前"+list);
        list.sort((a,b) -> b.length() - a.length());
        System.out.println("排序后(长到短)"+list);
        list.sort((a,b) -> a.length() - b.length());
        System.out.println("排序后(短到长)"+list);


    }

    /**
     * 查找List中特定元素的索引
     */
    public static void question05(){

        List<Integer> list = new ArrayList<>(Arrays.asList(4, 1, 6, 2, 3, 3, 4, 4));

        System.out.println("请输入特定的元素");
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();

        System.out.println("特定元素的索引"+list.indexOf(i));



    }

    /**
     * 去除List中重复的元素
     */
    public static void question06(){
        List<Integer> list = new ArrayList<>(Arrays.asList(4, 1, 6, 2, 3, 3, 4, 4));
        System.out.println("去重前"+list);
        HashSet<Integer> hs = new HashSet<>(list);
        System.out.println("去重后"+hs);

    }

    /**
     * 判断两个Set是否有交集
     */
    public static void question07(){

        Set<String> set = new HashSet<>();
        Set<String> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();

        set1.add("ss");
        set1.add("ff");
        set1.add("aa");
        set1.add("xx");

        set2.add("ff");
        set2.add("hh");
        set2.add("gg");
        set2.add("ll");

        set.clear();
        set.addAll(set1);
        set.retainAll(set2);

        if (set.size()!=0){
            System.out.println("有交集");
        } else {
            System.out.println("没有交集");
        }

    }

    /**
     * 找出两个Set的差集
     */
    public static void question08(){

        Set<String> set = new HashSet<>();
        Set<String> see = new HashSet<>();
        Set<String> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();

        set1.add("ss");
        set1.add("ff");
        set1.add("aa");
        set1.add("xx");

        set2.add("ff");
        set2.add("hh");
        set2.add("gg");
        set2.add("ll");

        set.clear();
        set.addAll(set1);
        set.removeAll(set2);

        see.clear();
        see.addAll(set2);
        see.removeAll(set1);

        set.addAll(see);

        System.out.println("两个Set的差集为"+set);

    }

    /**
     * 使用Set找出数组中的重复元素
     */
    public static void question09(){

        int[] num = new int[]{1,2,3,4,2,4,5};
        Set<Integer> set = new HashSet<>();
        System.out.println("重复元素为");
        for (int i : num){
            if (set.contains(i)) {
                System.out.print(i+" ");
            } else {
                set.add(i);
            }
        }

    }



    public static void main(String[] args) {

//        question01();
//        question02();
//        question03();
//        question04();
//        question05();

//        question06();
//        question07();
//        question08();
//        question09();

    }



}
